#include "../inc/hart_master.h"
/**
 * @brief数据样品型号(气开\气关)
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
BOOL hart_master_command_254_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    *resp->data_p++ = req->data.command_254.model;
    return TRUE;
}

/**
 * @brief编写参数配置
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
BOOL hart_master_command_260_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    float32_u f;
    uint16_t d16;
    hart_command_260_t *command = (hart_command_260_t *)&req->data.command_260;
    // 控制模式
    *resp->data_p++ = command->control_mode;
    // 阀位控制
    f.f = command->threshold_control;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 放大器回路控制
    f.f = command->amplifier_circuit_control;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // A口压力
    d16 = S2B_UINT16(command->pressure_A);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // B口压力
    d16 = S2B_UINT16(command->pressure_B);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 位置作用选择
    *resp->data_p++ = command->position_selector;
    // 位置反馈--P
    f.f = command->position_feedback_P;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈--I
    f.f = command->position_feedback_I;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈--D
    f.f = command->position_feedback_D;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈-放大器回路控制增益
    d16 = S2B_UINT16(command->position_feedback_amplifier_circuit_control_gain);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 压力作用选择
    *resp->data_p++ = command->pressure_selector;
    // 压力反馈--P
    f.f = command->pressure_feedback_P;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈--I
    f.f = command->pressure_feedback_I;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈--D
    f.f = command->pressure_feedback_D;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈-放大器回路控制增益
    d16 = S2B_UINT16(command->pressure_feedback_amplifier_circuit_control_gain);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 阀门型号
    osel_memcpy(resp->data_p, (uint8_t *)command->valve_model, ARRAY_LEN(command->valve_model));
    resp->data_p += ARRAY_LEN(command->valve_model);
    // 弹簧系数
    f.f = command->spring_coefficient;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 气室活塞面积
    f.f = command->gas_inlet_cross_sectional_area;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 气开/气关
    *resp->data_p++ = command->gas_opening_closing;

    return TRUE;
}

/**
 * @brief 用户测试指令
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
BOOL hart_master_command_500_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    *resp->data_p++ = req->data.command_500.test_code;
    *resp->data_p++ = req->data.command_500.data_length;
    if (req->data.command_500.data_length > 0)
    {
        osel_memcpy(resp->data_p, req->data.command_500.data, req->data.command_500.data_length);
        resp->data_p += req->data.command_500.data_length;
    }
    return TRUE;
}

BOOL hart_master_command_130_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    *resp->data_p++ = req->data.command_130.code;
    return TRUE;
}

BOOL hart_master_command_141_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    *resp->data_p++ = req->data.command_141.write_protect_code;
    return TRUE;
}

/**
 * @brief 用户自定义指令初始化
 * @return {*}
 */
void hart_master_req_init_user(void)
{
    hart_command_ptr_arr[HART_COMMAND_130] = hart_master_command_130_req;
    hart_command_ptr_arr[HART_COMMAND_141] = hart_master_command_141_req;
    // 254-511 自定义指令
    hart_command_ptr_arr[HART_COMMAND_254] = hart_master_command_254_req; // 数据样品型号(气开\气关)
    hart_command_ptr_arr[HART_COMMAND_260] = hart_master_command_260_req; // 编写参数配置
    hart_command_ptr_arr[HART_COMMAND_500] = hart_master_command_500_req; // 测试指令
}
